package panda.query.tree;

import panda.file.Page;
import panda.query.scan.Scanner;
import panda.query.struct.Attribute;
import panda.query.util.TempTable;
import panda.record.Schema;
import panda.record.TableToken;
import panda.transaction.Transaction;

/**
 * 
 * @author Tian Yuan
 */
public class MaterializeNode implements QueryTreeNode{
	private QueryTreeNode src;
	private Transaction tx;
	/**
	 * 
	 * @param src
	 */
	public MaterializeNode(QueryTreeNode src, Transaction tx) {
		this.src = src;
		this.tx = tx;
	}
	
	/**
	 * 
	 */
	public Scanner open() {
		TempTable tt = new TempTable(src.getSchema(), tx);
		
		tt.copyFrom(src.open());
		return tt.open();
	}

	/**
	 * 
	 */
	public int blockAccessed() {
		double t = (double)Page.BLOCK_SIZE / TableToken.getTupleLength(src.getSchema());
		return (int)(Math.ceil(src.outputSize() / t));
	}

	/**
	 * 
	 */
	public int distinctSize(Attribute attr) {
		return src.distinctSize(attr);
	}
	
	/**
	 * 
	 */
	public int outputSize() {
		return src.outputSize();
	}
	
	/**
	 * 
	 */
	public Schema getSchema() {
		return src.getSchema();
	}

}
